package com.xxl.job.executor.process;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.SpringContextUtil;
import com.xxl.job.executor.model.GatherMjModel;
import com.xxl.job.executor.model.SignalMjDhModel;
import com.xxl.job.executor.protocol.MjWebsocketClient;
import com.xxl.job.executor.service.DhService;
import com.xxl.job.executor.service.GatherMjService;
import com.xxl.job.executor.service.MjService;

import java.util.List;
import java.util.Map;

/**
 * 文件描述
 *
 * @author xianjin.lu
 * @date 2021
 */
public class MjProcess {


    //param  是  gatherId
    private String gatherId;

    public MjProcess(String param) {
        this.gatherId = param;
    }

    public void collectProcessor() throws Exception {
        for (int i = 0; i < 50000; i++) {

            XxlJobLogger.log("*************************************第"+i+"次******************************");


            // SpringContextUtil.getBean(GatherDefService.class)   相当于  Autowise  GatherDefService
            // redis中 查询 有无 gatherid  对应的  数据
           List<GatherMjModel> mjgatherList = SpringContextUtil.getBean(GatherMjService.class).findGatherMjModelList(gatherId);


            if (mjgatherList == null || mjgatherList.size() <= 0) {
                XxlJobLogger.log("未获取到门禁[" + gatherId + "]的配置信息！采集退出...");
                return;
            }else{

                //请求体
                //{cmdSn='0001', result=0, resultDesc='succ', resultData=[ResultData{devAddr='10.0.9.101', devPort='6000',
                // devId='1', devType='1', devPos='1', doorNum=1, areaName='区域1'}

                MjWebsocketClient websocketClient = new MjWebsocketClient();

                //获取gatherId倒数第二个字符
                char two = gatherId.charAt(gatherId.length() - 2);
                int twoint=two-48;

                //获取gatherId倒数第一个个字符
                char one = gatherId.charAt(gatherId.length() - 1);
                int oneint=one-48;

                  if(twoint==0){
                   String  s=   "{'head':{'cmdType':2,'cmdSn':'0002'},'data':{'devId':"+oneint+",'operType':0,'doorIndex':0}}";

                    websocketClient.find(s);
                  }else{
                      int total=twoint*10+oneint;
                      String  s=   "{'head':{'cmdType':2,'cmdSn':'0002'},'data':{'devId':"+total+",'operType':0,'doorIndex':0}}";
                      websocketClient.find(s);
                  }

                  int time=0;

                while(websocketClient.getMap().isEmpty()){
                    System.out.println("正在等待获取消息");
                    Thread.sleep(300);
                    time++;
                    if (time==20){
                        break;
                    }
                }


                Map<String, Object> map=websocketClient.getMap();

                //关闭websocket连接
                websocketClient.client.close();

                if (map.isEmpty()){
                    XxlJobLogger.log("门禁信息获取超时.请联系管理员");
                }else{
                    SignalMjDhModel automaticControlModel = new SignalMjDhModel();

                    for (String s : map.keySet()) {
                        String openState = map.get(s).toString();
                        automaticControlModel.setGatherId(gatherId);
                        automaticControlModel.setGatherResId(gatherId+"0001");
                        automaticControlModel.setSignalType("MJ");//设备状态信号
                        automaticControlModel.setPointAddr(s);   //设置pointAddr  对应设备id
                        automaticControlModel.setSignalValue(openState);  //设置信号值
                        switch (s) {    //设置设备编码
                            case "1":
                                automaticControlModel.setDevCode("XF-1427-J001-06-0271");
                                break;
                            case "2":
                                automaticControlModel.setDevCode("XF-1427-J001-07-0271");
                                break;
                            case "3":
                                automaticControlModel.setDevCode("XF-1427-C001-08-0271");
                                break;
                            case "4":
                                automaticControlModel.setDevCode("XF-1427-C001-08-0272");
                                break;
                            case "5":
                                automaticControlModel.setDevCode("XF-1427-C002-08-0271");
                                break;
                            case "6":
                                automaticControlModel.setDevCode("XF-1427-C002-07-0272");
                                break;
                            case "7":
                                automaticControlModel.setDevCode("XF-1427-C002-07-0273");
                                break;
                            case "8":
                                automaticControlModel.setDevCode("XF-1427-J002-06-0271");
                                break;
                            case "9":
                                automaticControlModel.setDevCode("XF-1427-J002-07-0271");
                                break;
                            case "11":
                                automaticControlModel.setDevCode("XF-1427-J003-06-0271");
                                break;
                            case "12":
                                automaticControlModel.setDevCode("XF-1427-J003-07-0271");
                                break;
                            case "15":
                                automaticControlModel.setDevCode("XF-1497-J001-06-0271");
                                break;
                            case "16":
                                automaticControlModel.setDevCode("XF-1497-J001-07-0271");
                                break;
                            case "17":
                                automaticControlModel.setDevCode("XF-1497-J002-06-0271");
                                break;
                            case "18":
                                automaticControlModel.setDevCode("XF-1497-J002-07-0271");
                                break;
                            default:
                                System.out.println("没有找到对应设备");
                        }

                    }

                    SpringContextUtil.getBean(MjService.class).save(automaticControlModel);
                    XxlJobLogger.log("............数据解析结束................");


                }
            }
        }
    }
}